package com.example.prom.controller;

import com.example.prom.converter.OrderModelConverter;
import com.example.prom.model.Order;
import com.example.prom.service.OrderService;
import com.example.prom.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping(value = "/order")
    public ResponseEntity<Boolean> createOrder(@RequestBody Order o) throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            int nextInt = random.nextInt(3000);
            System.out.println("睡眠："+nextInt+" 毫秒");
            Thread.sleep(nextInt);
            String[] channels = o.getChannel().split(",");
            Boolean bool = false;
            for (String channel : channels) {
                if ("mw".equalsIgnoreCase(channel)){
                    OrderVO order = OrderModelConverter.INSTANCE.sourceToTarget(o);
                    order.setChannel("mw");
                    order.setOrderId("mw"+order.getOrderId());
                    order.setTemplate("template0");
                    order.setAreacode("86");
                    bool = orderService.createOrder1(order);
                } else if ("sw".equalsIgnoreCase(channel)){
                    OrderVO order = OrderModelConverter.INSTANCE.sourceToTarget(o);
                    order.setChannel("sw");
                    order.setOrderId("sw"+order.getOrderId());
                    order.setTemplate("template0");
                    order.setAreacode("86");
                    bool = orderService.createOrder2(order);
                } else if ("jm".equalsIgnoreCase(channel)){
                    OrderVO order = OrderModelConverter.INSTANCE.sourceToTarget(o);
                    order.setChannel("jm");
                    order.setOrderId("jm"+order.getOrderId());
                    order.setTemplate("template0");
                    order.setAreacode("86");
                    bool = orderService.createOrder3(order);
                } else if ("twilio".equalsIgnoreCase(channel)){
                    OrderVO order = OrderModelConverter.INSTANCE.sourceToTarget(o);
                    order.setChannel("twilio");
                    order.setOrderId("twilio"+order.getOrderId());
                    order.setTemplate("template0");
                    order.setAreacode("86");
                    bool = orderService.createOrder4(order);
                }
            }
        }
        return ResponseEntity.ok(true);

    }

}
